<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    Mirze<mirze@foxmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_auth_mc_log.class.php 2011-07-07 11:56:30 $
 * @Show Note: 设备验收记录表
 */
Class zy_auth_mc_log extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;
	
	/**
	 *	Construct this module class.
	*/
	public function zy_auth_mc_log()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_auth_mc_log.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_auth_mc_log");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		$this->smarty->assign("main_menus",$this->get_user_menus());
	}
	
	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
			$this->zy_auth_mc_log();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$col = "auth_mc_log_id";
		$where = 1;
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);
		
		$this->smarty->assign('arr',$arr);
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->assign('act','list');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	add method.
	*/
	public function add()
	{
		//合同号
		$contract_no = isset($_REQUEST['contract_no']) ? trim($_REQUEST['contract_no']) : '' ;
		//验收ID
		$mc_auth_id = isset($_REQUEST['mc_auth_id']) ? trim($_REQUEST['mc_auth_id']) : '' ;
		if(empty($contract_no)) {
			alertMsg2("合同号为空", 'index.php?c=zy_sell');
		}
		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			$data_list = $this->db->getEquipment($contract_no);
			//dump($data_list);
			if(empty($data_list)) {
				alertMsg2("该合同下无验收设备", 'index.php?c=zy_mc_auth');
			}
			$this->smarty->assign('contract_no',$contract_no);
			$this->smarty->assign('mc_auth_id',$mc_auth_id);
			$this->smarty->assign('data_list',$data_list);

			$this->smarty->assign('auth_result_info',get_auth_result());//验收结果

			//使用模版，展示提交界面
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//验收通过: 记录"验收通过设备"
			$db_zafm = Factory::getDB('zy_auth_finish_mc');
			//用户提交，把数据插入数据库
			$temp = $this->getValue();
			//出场编号和设备自编号检查
			$where = "comp_no='{$temp['comp_no']}' and auth_result=1";
			$comp_no_arr = $this->db->getAll($where);
			if(isset($comp_no_arr[0])){
				alertMsg('-3002');
			}

			$finsh_where = "mc_self_no='{$_POST['mc_self_no']}'";
			$finsh_arr = $db_zafm->getAll($finsh_where);
			//print_r($db_zafm->getDebug());exit;
			if(isset($finsh_arr[0])){
				alertMsg('-3003');
			}
			$insrt = $this->db->insert_zy_auth_mc_log($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			$mc_auth_id = $temp['mc_auth_id'];

			if($insrt > 0 && $temp['auth_result'] == 1) {//添加成功及通过验证更新
				$temp['mc_self_no'] = $_POST['mc_self_no'];
				$temp['mc_to_project'] = $_POST['mc_to_project'];
				$temp['put_user_id'] = $_POST['put_user_id'];
				
				$val_zafm = array(
					'comp_no' => $temp['comp_no'],
					'mc_state' => 1, //1已验收，2已转资，3已调拨
					'contract_no' => $contract_no,
					'mc_log_id' => $temp['mc_log_id'],
					'mc_self_no' => $temp['mc_self_no'],
					'mc_to_project' => $temp['mc_to_project'],
					'put_user_id' => $temp['put_user_id'],
					'auth_mc_log_id' => $insrt,
				);
				$insertId = $db_zafm->insert_zy_auth_finish_mc($val_zafm);
				//print_r($db_zafm->getDebug());exit;
				$db_znl = Factory::getDB('zy_no_log');
				$res = $db_znl->updateNoLog($temp['mc_log_id']);
				if(empty($res)) {//更新失败,删除记录
					$where = "auth_mc_log_id = '{$insrt}'";
					$delId = $this->db->delete_zy_auth_mc_log($where);
					alertMsg2("添加失败", 'index.php?c=zy_mc_auth');
				}
			}
			//alertMsg($error_no, 'index.php?c=zy_mc_auth');
			if(0 < $insrt) { //成功直接跳转
				redirectPage("index.php?c=zy_mc_auth&m=browse&mc_auth_id={$mc_auth_id}");
			} else {
				alertMsg($error_no, 'index.php?c=zy_mc_auth');
			}
		}
	}
	
	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$auth_mc_log_id = isset($_REQUEST['auth_mc_log_id']) ? intval(trim($_REQUEST['auth_mc_log_id'])) : 0 ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//合同号
			$contract_no = isset($_REQUEST['contract_no']) ? trim($_REQUEST['contract_no']) : '' ;
			//使用模版，展示提交界面
			$result = $this->db->getOne($auth_mc_log_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_auth_mc_log');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}

			$data_list = $this->db->getEquipment($contract_no);
			$this->smarty->assign('data_list',$data_list);

			$this->smarty->assign('auth_result_info',get_auth_result());//验收结果

			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "auth_mc_log_id=$auth_mc_log_id";
			$temp = $this->getValue(1);			
			$update = $this->db->update_zy_auth_mc_log($temp,$where);
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			//alertMsg($error_no, 'index.php?c=zy_auth_mc_log');
			if(0 < $update) { //成功直接跳转
				redirectPage("index.php?c=zy_mc_auth&m=browse&mc_auth_id={$temp['mc_auth_id']}");
			} else {
				alertMsg($error_no, 'index.php?c=zy_mc_auth');
			}
		}
	}
	
	/**
	 *	browse method.
	*/
	public function browse() {
		$auth_mc_log_id = isset($_REQUEST['auth_mc_log_id']) ? intval(trim($_REQUEST['auth_mc_log_id'])) : 0 ;
		$mc_auth_id = isset($_REQUEST['mc_auth_id']) ? intval(trim($_REQUEST['mc_auth_id'])) : 0 ;
		//使用模版，展示提交界面
		$result = $this->db->getOne($auth_mc_log_id);
		if(empty($result))
		{
			alertMsg('-2001', 'index.php?c=zy_auth_mc_log');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}

		//对应验收记录
		$db_zaml = Factory::getDB('zy_auth_mc_log');
		//$recordArr =  $db_zaml->getJoinRecord($mc_auth_id);
		$recordArr =  $db_zaml->getDeviceInfo($mc_auth_id, $auth_mc_log_id);
		//print_r($recordArr);
		$this->smarty->assign('recordArr',$recordArr[0]);

		$this->smarty->assign('auth_result_info',get_auth_result());//验收结果

		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$auth_mc_log_id = isset($_REQUEST['auth_mc_log_id']) ? intval(trim($_REQUEST['auth_mc_log_id'])) : 0 ;
		$mc_auth_id  = isset($_REQUEST['mc_auth_id']) ? intval(trim($_REQUEST['mc_auth_id'])) : 0 ;
		$where = "auth_mc_log_id=$auth_mc_log_id";
		$del=$this->db->del_finish_mc($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		//alertMsg($error_no, 'index.php?c=zy_auth_mc_log');
		if ($del < 0) {
			alertMsg($error_no, 'index.php?c=zy_mc_auth');
		}else{
			//如果删除了，则将验收数量减少一个
			
			redirectPage("index.php?c=zy_mc_auth&m=browse&mc_auth_id={$mc_auth_id}");
		}		
	}

	/**
	 *	import method.
	 */
	public function imports()
	{
		$opt = isset($_POST['opt']) ? intval($_POST['opt']) : 0 ;
		$obj_id = isset($_REQUEST['obj_id']) ? intval($_REQUEST['obj_id']) : 0 ; //mc_auth_id
		if($obj_id <= 0) {
			alertMsg2("操作有误", 'index.php?c=zy_mc_auth');
		}

		$db_ztt = Factory::getDB('zy_mc_auth');
		$info = $db_ztt->getOne($obj_id);
		$contract_no = isset($info['contract_no']) ? trim($info['contract_no']) : ''; //合同号
		$data_list = $this->db->getEquipment($contract_no);

		if(empty($opt))	{			
			$obj_lab_val = isset($info['mc_auth_no']) ? trim($info['mc_auth_no']) : '';

			$this->smarty->assign('act_title', "设备验收-验收设备明细导入");
			$this->smarty->assign('act', 'imports');
			$this->smarty->assign('control', 'zy_auth_mc_log');
			$this->smarty->assign('obj_id', $obj_id);
			$this->smarty->assign('obj_lab', "验收报告编号");
			$this->smarty->assign('obj_lab_val', $obj_lab_val);
			$this->smarty->display($this->tplFile);
		} else {
			//1. 上传xls
			$savePath = UPLOAD_PATH . 'csv/zy_auth_mc_log/';
			if(!file_exists($savePath)) {
				mk_dir($savePath);
			}
			$uploadRes = $this->upload_file_xls($savePath, 'fileName', array('csv'));
			if(0 == $uploadRes['state']) {
				alertMsg2($uploadRes['msg'], "index.php?c=zy_mc_auth&m=browse&mc_auth_id={$obj_id}");
			}
			$fileName = $uploadRes['msg']; //上传成功后文件名

			//2. 上传成功后读取xls数据分析
			$item_pe = Factory::getItemClass('parse_excel');

			//获取xls文件数据
			$filePath = $savePath.$fileName;
			//$arr_xls = $item_pe->data_xls($filePath);
			//$out = $item_pe->data_parse_xls($arr_xls);

			$arr_csv = $item_pe->data_csv($filePath);
			$out = is_array($arr_csv['data']) ? $arr_csv['data'] : array();

			if(empty($out) || count($out) < 2) {
				alertMsg2("读取文件内容失败或数据为空", "index.php?c=zy_mc_auth&m=browse&mc_auth_id={$obj_id}");
			}
			unset($out[0]); //去掉标题栏

			//3. 入库: 24项，带表头，去第一列序号
			//$zaml = Factory::getDB('zy_auth_mc_log');
			$this->db_zafm = Factory::getDB('zy_auth_finish_mc');
			foreach($out as $k => $v) {
				if(!empty($v)) {
					$val['mc_auth_id'] = $obj_id; //
					//$val['admin_user_id'] = $this->admin_user_id; //录入用户：后台登录用户
					if(!empty($v[0]) && !empty($v[1])) {
						$val['mc_cn_name'] = $v[0];
						$val['comp_no'] = $v[1];
						$val['mc_self_no'] = $v[2];
						$val['mc_to_project'] = $v[3]; //调配去向
						$val['put_user_id'] = $v[4]; //分配人员
						$val['auth_result'] = $v[5];
						$val['auth_content'] = $v[6];
						$val['auth_info'] = $v[7];

						$ress = $this->import_add($val, $contract_no);
					}
					unset($val);
				}
			}
			//redirectPage('index.php?c=zy_mend');
			alertMsg2('导入完成', "index.php?c=zy_mc_auth&m=browse&mc_auth_id={$obj_id}");
		}
	}

	/**
	 * (功能描述) 分析、插入表数据
	 *
	 * @param 
	 * @return true/false
	 */
	public function import_add($temp, $no='')
	{
		//出场编号和设备自编号检查
		$where = "comp_no='". $temp['comp_no'] . "' and auth_result=1";
		$comp_no_arr = $this->db->getAll($where);
		if(isset($comp_no_arr[0])){
			return false; //-3002 出厂编号重复
		}

		$finsh_where = "mc_self_no='" . $temp['mc_self_no'] . "'";
		$finsh_arr = $this->db_zafm->getAll($finsh_where);
		if(isset($finsh_arr[0])){
			return false; //-3003 设备自编号已存在
		}

		$insrt = $this->db->insert_zy_auth_mc_log($temp);
		return $insrt >0 ? true :  false;
	}
	
	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		if($isEdit){
			$temp['auth_mc_log_id'] = $_POST['auth_mc_log_id'];
		} else {
			$temp['auth_mc_log_id'] = "";
		}
		//补充其他的字段
		$temp['auth_mc_log_info'] = $_POST['auth_mc_log_info'];
		$temp['comp_no'] = $_POST['comp_no'];
		$temp['mc_log_union'] = $_POST['mc_log_union'];
		$temp['auth_result'] = $_POST['auth_result'];
		$temp['auth_content'] = $_POST['auth_content'];
		$temp['auth_info'] = $_POST['auth_info'];
		$temp['auth_no'] = isset($_POST['auth_no']) ? intval($_POST['auth_no']) : 0;
		$temp['mc_auth_id'] = $_POST['mc_auth_id'];
		$temp['mc_log_id'] = $_POST['mc_log_id'];


		if(empty($temp['auth_no'])) {
			alertMsg2("请正确填写验收数量");
		}
				
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
			/* 定义该类中文名称*/
			'class_name'=>array(__CLASS__,'验收记录'),
			'class_method' =>array(
				//array（方法名，中文解释，权值,是否为菜单）
				'show'  => array('show','列表',1,true),
				'add'  => array('add','添加',2,true),
				'edit'  => array('edit','编辑',4,false),
				'del'  => array('del','删除',8,false),
				'browse' => array('browse', '查看', 16, false),
				//'imports'  => array('imports','导入',32,false),
				'show_p'=> array('show_p','',0,false)
			)
		);
	}
}

?>